$(function () {
   $('#save-banner-btn').click(function (event) {
       event.preventDefault();
       var self = $(this);
       var dialog = $('#myModal');
       var nameE = $('input[name="name"]');
       var image_urlE = $('input[name="image_url"]');
       var link_urlE = $('input[name="link_url"]');
       var priorityE = $('input[name="priority"]');

       var name = nameE.val();
       var image_url = image_urlE.val();
       var link_url = link_urlE.val();
       var priority = priorityE.val();
       var submitType = self.attr('data-type');
       var bannerId = self.attr('data-id');

       var url='';
       if (submitType == 'update'){
           url = '/cms/ubanner/' ;
       }else {
           url = '/cms/abanner/'
       }
       zlajax.post({
           'url' : url ,
           'data' : {
               'name' : name,
               'image_url' : image_url,
               'link_url' : link_url ,
               'priority' :priority,
               'banner_id' : bannerId
           },
           'success' : function (data) {
               if (data['code'] == 200){
                   //隐藏模态框
                   $('#myModal').modal('hide');
                   //重新加载页面
                   window.location.reload()
               }else {
                   xtalert.alertInfo(data['message']);
               }
           },
           'fail' : function (error) {
               xtalert.alertNetworkError('网络错误！')
           }
       });
   }) ;
});

$(function () {
   $('.edit-banner-btn').click(function (event) {
       var self = $(this);
       var dialog = $('#myModal');
       dialog.modal('show');

       var tr = self.parent().parent();
       var name = tr.attr('data-name');
       var image_url = tr.attr('data-image_url');
       var link_url = tr.attr('data-link_url');
       var priority = tr.attr('data-priority');

       var nameE = dialog.find('input[name="name"]');
       var image_urlE = dialog.find('input[name="image_url"]');
       var link_urlE = dialog.find('input[name="link_url"]');
       var priorityE = dialog.find('input[name="priority"]');
       var saveBtn = dialog.find('#save-banner-btn');


       nameE.val(name);
       image_urlE.val(image_url);
       link_urlE.val(link_url);
       priorityE.val(priority);

       saveBtn.attr('data-type','update');
       saveBtn.attr('data-id',tr.attr('data-id'));
   }) ;
});

$(function () {
   $('.delete-banner-btn').click(function (event) {
      var self = $(this);
      var tr = self.parent().parent();
      var banner_id = tr.attr('data-id');
      xtalert.alertConfirm({
          'msg' : '确定要删除这个轮播图吗？',
          'confirmCallback' : function () {
              zlajax.post({
                  'url' : '/cms/dbanner/',
                  'data' : {
                      'banner_id' : banner_id
                  },
                  'success' : function (data) {
                      if(data['code'] == 200){
                          xtalert.alertSuccessToast('删除成功！');
                          window.location.reload();
                      }else {
                          xtalert.alertInfo(data['message']);
                      }
                  },
                  'fail' : function (error) {
                      xtalert.alertNetworkError('网络错误！');
                  }
              })
          }
      });
   });
});

$(function () {
   zlqiniu.setUp({
       'domain' : 'http://pen0pyon9.bkt.clouddn.com/',
       'browse_button' : 'upload-btn',
       'uptoken_url' : '/c/uptoken/',
       'success' : function (up,file,info) {
           var imageInput = $('input[name="image_url"]');
           imageInput.val(file.name) ;
       }
   })
});